package com.remote.core;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.remote.config.AppConfiguration;

import ch.ethz.ssh2.Connection;

@Service
public class RemoteCommandTemplete {
	private static final Logger log = LoggerFactory.getLogger(RemoteCommandTemplete.class);
	@Autowired
	private AppConfiguration appConfiguration;
	public Connection getConnection(String ip, String username, String address) {
		Connection conn;
		boolean flg = false;
		try {
			conn = new Connection(ip);
			conn.connect();// 连接
			flg = conn.authenticateWithPassword(username,address);// 认证
			if (flg) {
				return conn;
			}
		} catch (IOException e) {
			log.error("=========登录失败=========" + e.getMessage());
			e.printStackTrace();
		}
		return null;
	}

	public Connection getConnection() {
      return this.getConnection(appConfiguration.getTencentCloudIp(), appConfiguration.getTencentCloudUsername(),
				appConfiguration.getTencentCloudPasswd());
	}
}
